<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="宠物姓名" prop="petName">
        <el-input
          v-model="queryParams.petName"
          placeholder="请输入宠物姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="描述信息" prop="petDescription">-->
<!--        <el-input-->
<!--          v-model="queryParams.petDescription"-->
<!--          placeholder="请输入描述信息"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="创建日期" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['adopt:situation:edit']"
        >详情</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" @change="changeHeight"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="situationList" :height="tableHeight" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="宠物姓名" align="center" prop="petName" />
      <el-table-column label="照片" align="center" prop="petPhoto" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.petPhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="描述信息" align="center" prop="petDescription" />
      <el-table-column label="创建日期" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改近况对话框 -->
    <el-dialog title="宠物近况详情" :visible.sync="open" width="800px" style="margin-top: 100px" class="max-width-limit" append-to-body>
      <el-descriptions style="margin-left: 25px;margin-top: -30px">
        <el-descriptions-item label="宠物昵称">
          {{ form.petName }}
        </el-descriptions-item>
        <el-descriptions-item label="创建日期" content-class-name="long_desc_content">
          {{ form.createTime }}
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions style="margin-left: 25px;margin-top: -5px" :column="1">
        <el-descriptions-item label="描述信息" content-class-name="long_desc_content">
          {{ form.petDescription }}
        </el-descriptions-item>
        <el-descriptions-item label="照片" style="margin-left: -20px">
          <div style="width: 650px;margin-top: 5px">
            <div style="width: 160px;float:left" v-for="photo in form.petPhoto">
              <image-preview :src="photo" :width="150" :height="150"/>
            </div>
          </div>
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSituation, getSituation, delSituation, addSituation, updateSituation } from "@/api/adopt/situation";

export default {
  name: "Situation",
  data() {
    return {
      tableHeight:320,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 近况表格数据
      situationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        petId: null,
        petName: null,
        createTime:null,
        petPhoto: "",
        petDescription: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  // 表格高度自适应
  mounted () {
    this.$nextTick(function () {
      this.getHeight();
    })
    window.addEventListener('resize',this.getHeight,false)
  },
  methods: {
    changeHeight(){
      this.$nextTick(function () {
        this.getHeight();
      })
    },
    getHeight(){
      //listView
      const _this=this;
      let height1=document.defaultView.getComputedStyle(_this.$el, null)["padding-top"];
      height1=height1.replace("px","");
      let height2=document.getElementsByClassName("el-table")[0].offsetTop;
      _this.tableHeight=document.documentElement.clientHeight-height2-height1-150+"px";
    },
    /** 查询近况列表 */
    getList() {
      this.loading = true;
      listSituation(this.queryParams).then(response => {
        this.situationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recentSituationId: null,
        petId: null,
        petName: null,
        petPhoto: "",
        petDescription: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recentSituationId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.petId=this.$route.query.petId;
      console.log("petDescription="+this.form.petDescription);
      this.open = true;
      this.title = "添加近况";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recentSituationId = row.recentSituationId || this.ids
      getSituation(recentSituationId).then(response => {
        this.form = response.data;
        this.form.petPhoto = this.form.petPhoto.split(',');
        this.open = true;
        this.title = "修改近况";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recentSituationId != null) {
            updateSituation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSituation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recentSituationIds = row.recentSituationId || this.ids;
      this.$modal.confirm('是否确认删除近况编号为"' + recentSituationIds + '"的数据项？').then(function() {
        return delSituation(recentSituationIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('adopt/situation/export', {
        ...this.queryParams
      }, `situation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.desc_content {
  margin-left: 20px;
  background: #E1F3D8;
}
.long_desc_content {
  display: block;
  background: #E1F3D8;
}
::v-deep{
  .el-descriptions-item__container {
    margin-top: 30px;
  }
}
</style>
